package com.vainycos.lotterysearch.service;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vainycos.lotterysearch.mapper.WorldCupDetailMapper;
import com.vainycos.lotterysearch.model.WorldCupDetailDO;
import com.vainycos.lotterysearch.model.ro.WorldCupDetailRo;
import com.vainycos.lotterysearch.model.worldcup.CompetitionData;
import com.vainycos.lotterysearch.model.worldcup.Worldcup;
import com.vainycos.lotterysearch.utils.FreeApiUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Vainycos
 * @description
 * @date: 2022/11/22 13:40
 */
@Service
public class WorldCupDetailService extends ServiceImpl<WorldCupDetailMapper, WorldCupDetailDO> {

    @Autowired
    private WorldCupDetailMapper worldCupDetailMapper;

    /**
     * 初始化详情比赛数据（先清空，再进行全量更新）
     */
    public boolean initDetail(){
        worldCupDetailMapper.truncate();
        List<WorldCupDetailDO> worldCupDetailDOS = new ArrayList<>();
        List<Worldcup> allWorldcupInfo = FreeApiUtil.getAllWorldcupInfo();
        allWorldcupInfo.forEach(worldcup->{
            CompetitionData competitionData = worldcup.getData().get(0);
            competitionData.getList().forEach(c->{
                // 当天的每一场比赛
                WorldCupDetailDO worldCupDetailDO = new WorldCupDetailDO();
                worldCupDetailDO.setTime(competitionData.getTime());
                worldCupDetailDO.setDate(c.getDate());
                worldCupDetailDO.setStartTime(c.getStartTime());
                worldCupDetailDO.setMatchName(c.getMatchName());
                worldCupDetailDO.setStatus(c.getStatus());
                worldCupDetailDO.setStatusText(c.getStatusText());
                worldCupDetailDO.setHot(c.getHot());
                worldCupDetailDO.setLeftName(c.getLeftLogo().getName());
                worldCupDetailDO.setLeftLogo(c.getLeftLogo().getLogo());
                worldCupDetailDO.setLeftScore(c.getLeftLogo().getScore());
                worldCupDetailDO.setRightName(c.getRightLogo().getName());
                worldCupDetailDO.setRightLogo(c.getRightLogo().getLogo());
                worldCupDetailDO.setRightScore(c.getRightLogo().getScore());
                worldCupDetailDO.setContent(JSONObject.toJSONString(c));
                worldCupDetailDOS.add(worldCupDetailDO);
            });
        });
        return saveBatch(worldCupDetailDOS);
    }

    /**
     * 根据时间查询列表
     * @param time
     * @return
     */
    public List<WorldCupDetailDO> getDetailListByTime(String time){
        LambdaQueryWrapper<WorldCupDetailDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(WorldCupDetailDO::getTime, time);
        lambdaQueryWrapper.orderByAsc(WorldCupDetailDO::getStartTime);
        return worldCupDetailMapper.selectList(lambdaQueryWrapper);
    }

    /**
     *
     * @return
     */
    public List<WorldCupDetailDO> getList(WorldCupDetailRo worldCupDetailRo){
        // 只查询未开赛的列表
        return worldCupDetailMapper.selectList(new LambdaQueryWrapper<WorldCupDetailDO>().eq(worldCupDetailRo.getStatus() != null, WorldCupDetailDO::getStatus, worldCupDetailRo.getStatus()));
    }

    /**
     * 计算热度值排名
     * @param startTime
     * @return
     */
    public int rank(String startTime){
        return worldCupDetailMapper.rank(startTime);
    }
}
